package com.xx.sg.own.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xx.sg.exception.ServiceException;
import com.xx.sg.own.entity.Duty;
import com.xx.sg.own.entity.DutyUser;
import com.xx.sg.own.mapper.DutyMapper;
import com.xx.sg.own.mapper.DutyUserMapper;
import com.xx.sg.own.service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DutyServiceImpl extends ServiceImpl<DutyMapper, Duty> implements DutyService {

    @Autowired
    private DutyMapper dutyMapper;
    @Autowired
    private DutyUserMapper dutyUserMapper;

    @Override
    public void saveDuty(Duty duty) {

//        if (duty.getLeaderIds().isEmpty()) {
//            throw new ServiceException("值班领导不能为null");
//        }
        if (duty.getStaffIds().isEmpty()) {
            throw new ServiceException("值班人员不能为null");
        }
//        if (duty.getDirectorIds().isEmpty()) {
//            throw new ServiceException("局领导不能为null");
//        }

        LambdaQueryWrapper<Duty> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Duty::getDeptId, duty.getDeptId())
                        .eq(Duty::getDutyDate, duty.getDutyDate());
        if (!dutyMapper.selectList(wrapper).isEmpty()) {
            throw new ServiceException("该值班计划已添加。");
        }

        dutyMapper.insert(duty);

        for (String id : duty.getDirectorIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("director")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
        for (String id : duty.getStaffIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("staff")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
        for (String id : duty.getLeaderIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("leader")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
    }

    @Override
    public void modifyDuty(Duty duty) {
//        if (duty.getLeaderIds().isEmpty()) {
//            throw new ServiceException("值班领导不能为null");
//        }
        if (duty.getStaffIds().isEmpty()) {
            throw new ServiceException("值班人员不能为null");
        }
//        if (duty.getDirectorIds().isEmpty()) {
//            throw new ServiceException("局领导不能为null");
//        }

        dutyMapper.updateById(duty);

        LambdaUpdateWrapper<DutyUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DutyUser::getDutyId, duty.getId())
                .set(DutyUser::getDeleteFlag,1);
        dutyUserMapper.update(wrapper);

        for (String id : duty.getDirectorIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("director")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
        for (String id : duty.getStaffIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("staff")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
        for (String id : duty.getLeaderIds()) {
            DutyUser dutyUser = DutyUser.builder()
                    .dutyId(duty.getId())
                    .userId(id)
                    .userType("leader")
                    .build();
            dutyUserMapper.insert(dutyUser);
        }
    }

    @Override
    public List<Duty> selectDutyList(Duty duty) {
        return dutyMapper.getDutyList(duty);
    }

    @Override
    public Duty selectDutyById(String id) {
        return dutyMapper.getDutyById(id);
    }
}
